/*
 * BlobfinderFile.java
 *
 * Created on 17 de febrero de 2008, 04:18 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 *
 *
 *
 */


package Util;

import java.io.*;
import java.util.StringTokenizer;
import java.util.*;
/**
 *
 * @author alejo
 */
public class BlobfinderFile {

    private FileReader file=null;
    private String fileName=null;
    private String path=null;
    BufferedReader bf;
    
    /** Creates a new instance of BlobfinderFile */
    public BlobfinderFile(String fileName, String path) {
        this.fileName=fileName;
        this.path=path;
        
        
    }
    
    /*
     *Method used to create a FileReader class, which opens an existing file of system.
     *It uses the path and name passed in the constructor.
     *@param none
     *@return FileReader file
     */
    public FileReader createFile(){
        file=null;
        try{
           
            //creating file and buffer, in order to use tokenizers
            file=new FileReader(path + "/"+fileName);
            bf = new BufferedReader(file);
            
            //marking initial position
            bf.mark(500);
            
        }
        catch(Exception e){
            e.printStackTrace();
            return null;        
        }
        
        return file;        
    }
    
    public ColorBlob getColorObst()
    {
      
        String line;
        StringTokenizer st; 
        
        System.out.println("Reading obstacle color...");
        try{
            
            
            //resetting the buffer to the first position
            bf.reset();
                
            while(true)
            {
                
                line = bf.readLine();
                
                //end of file reached, no colors identified
                if(line.equals(null)){
                        return null;
                }
                else
                {
                    st = new StringTokenizer(line," ");
                    if(st.nextToken().equals("obstColor"))
                    {
                        String value = st.nextToken();
                        String name = st.nextToken();
                        System.out.println("Value: "+value+"\nName: "+name);
                        ColorBlob x = new ColorBlob(name, Integer.parseInt(value));
                        return x;
                    }
                    
                }
                  
            }
            
            
        }
        catch(Exception e)
        {
            e.printStackTrace();
            System.out.println("Could not read the file [BlobfinderFile]");
            return null;
        }
        
        
    }
    
    public int getMinArea()
    {

        
        String line;

        StringTokenizer st; 
        
        System.out.println("Reading obstacle minArea...");
        try{
           
            //resetting the buffer to the first position
            bf.reset();
            
            while(true)
            {

                line = bf.readLine();
                //end of file reached, no colors identified
                if(line.equals(null)){
                        return -1;
                }
                else
                {
                    st = new StringTokenizer(line," ");
                    if(st.nextToken().equals("minArea"))
                    {
                        String value = st.nextToken();
                        System.out.println("Value: "+value);
                        return Integer.parseInt(value);
                    }
                    
                }
                 
            }
            
            
        }
        catch(Exception e)
        {
            e.printStackTrace();
            System.out.println("Could not read the file [BlobfinderFile]");
            return -1;
        }
   
    }
    
    public int getMaxArea()
    {

        
        String line;

        StringTokenizer st; 
        
        System.out.println("Reading obstacle maxArea...");
        try{
           
            //resetting the buffer to the first position
            bf.reset();
            
            while(true)
            {

                line = bf.readLine();
                //end of file reached, no colors identified
                if(line.equals(null)){
                        return -1;
                }
                else
                {
                    st = new StringTokenizer(line," ");
                    if(st.nextToken().equals("maxArea"))
                    {
                        String value = st.nextToken();
                        System.out.println("Value: "+value);
                        return Integer.parseInt(value);
                    }
                    
                }
                 
            }
            
            
        }
        catch(Exception e)
        {
            e.printStackTrace();
            System.out.println("Could not read the file [BlobfinderFile]");
            return -1;
        }
   
    }
 
    public double[] getProyectionMatrix()
    {
        String line;

        StringTokenizer st; 
        
        System.out.println("Reading proyection matrix...");
        
        
        try{
           
            //resetting the buffer to the first position
            bf.reset();
            
            while(true)
            {

                line = bf.readLine();
                //end of file reached, no colors identified
                if(line.equals(null)){
                        return null;
                }
                else
                {
                    
                    st = new StringTokenizer(line," ");
                    if(st.nextToken().equals("matrix"))
                    {
                        double[] matrix = new double[9];
                        
                        for(int i=0;i<9;i++)
                        {
                            matrix[i]= Double.parseDouble(st.nextToken());
                            System.out.println("Value: "+matrix[i]);
                        }
                        return matrix;
                    }
                    
                }
                 
            }
            
            
        }
        catch(Exception e)
        {
            e.printStackTrace();
            System.out.println("Could not read the file [BlobfinderFile]");
            return null;
        }     
    }
 

public double[] getIntrinsicMatrix()
    {
        String line;

        StringTokenizer st; 
        
        System.out.println("Reading intrinsic matrix...");
        
        
        try{
           
            //resetting the buffer to the first position
            bf.reset();
            
            while(true)
            {

                line = bf.readLine();
                //end of file reached, no colors identified
                if(line.equals(null)){
                        return null;
                }
                else
                {
                    
                    st = new StringTokenizer(line," ");
                    if(st.nextToken().equals("intrinsic"))
                    {
                        double[] matrix = new double[9];
                        
                        for(int i=0;i<9;i++)
                        {
                            matrix[i]= Double.parseDouble(st.nextToken());
                            System.out.println("Value: "+matrix[i]);
                        }
                        return matrix;
                    }
                    
                }
                 
            }
            
            
        }
        catch(Exception e)
        {
            e.printStackTrace();
            System.out.println("Could not read the file [BlobfinderFile]");
            return null;
        }     
    }


public double[] getLensDistMatrix()
    {
        String line;

        StringTokenizer st; 
        
        System.out.println("Reading proyection matrix...");
        
        
        try{
           
            //resetting the buffer to the first position
            bf.reset();
            
            while(true)
            {

                line = bf.readLine();
                //end of file reached, no colors identified
                if(line.equals(null)){
                        return null;
                }
                else
                {
                    
                    st = new StringTokenizer(line," ");
                    if(st.nextToken().equals("matrix"))
                    {
                        double[] matrix = new double[2];
                        
                        for(int i=0;i<2;i++)
                        {
                            matrix[i]= Double.parseDouble(st.nextToken());
                            System.out.println("Value: "+matrix[i]);
                        }
                        return matrix;
                    }
                    
                }
                 
            }
            
            
        }
        catch(Exception e)
        {
            e.printStackTrace();
            System.out.println("Could not read the file [BlobfinderFile]");
            return null;
        }     
    }
}